package com.luoboduner.moo.info;

import com.formdev.flatlaf.extras.FlatDesktop;
import com.formdev.flatlaf.extras.FlatInspector;
import com.formdev.flatlaf.extras.FlatUIDefaultsInspector;
import com.formdev.flatlaf.util.SystemInfo;
import com.luoboduner.moo.info.ui.Init;
import com.luoboduner.moo.info.ui.dialog.AboutDialog;
import com.luoboduner.moo.info.ui.dialog.SettingDialog;
import com.luoboduner.moo.info.ui.form.LoadingForm;
import com.luoboduner.moo.info.ui.form.MainWindow;
import com.luoboduner.moo.info.ui.frame.MainFrame;
import com.luoboduner.moo.info.util.ConfigUtil;
import com.luoboduner.moo.info.util.UIUtil;
import com.luoboduner.moo.info.util.UpgradeUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;

import javax.swing.*;
import java.awt.*;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Main Enter!
 *
 * @author <a href="https://github.com/rememberber">RememBerBer</a>
 * @since 2021/11/07.
 */
@Slf4j
public class App {

  public static ConfigUtil config = ConfigUtil.getInstance();

  public static MainFrame mainFrame;

  public static oshi.SystemInfo si;
  private static ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 40, 2, TimeUnit.MINUTES, new LinkedBlockingDeque<>());

  public static void main(String[] args) {

    if (SystemInfo.isMacOS) {
//            java -Xdock:name="MooInfo" -Xdock:icon=MooInfo.jpg ... (whatever else you normally specify here)
//            java -Xms64m -Xmx256m -Dapple.awt.application.name="MooInfo" -Dcom.apple.mrj.application.apple.menu.about.name="MooInfo" -cp "./lib/*" com.luoboduner.moo.info.App
      System.setProperty("apple.laf.useScreenMenuBar", "true");
      System.setProperty("apple.awt.application.name", "DaisyAgent");
      System.setProperty("com.apple.mrj.application.apple.menu.about.name", "DaisyAgent");
      System.setProperty("apple.awt.application.appearance", "system");

      //FlatDesktop.setAboutHandler(() -> {
      //  try {
      //    AboutDialog dialog = new AboutDialog();
      //
      //    dialog.pack();
      //    dialog.setVisible(true);
      //  }
      //  catch (Exception e2) {
      //    log.error(ExceptionUtils.getStackTrace(e2));
      //  }
      //});
      FlatDesktop.setPreferencesHandler(() -> {
        try {
          SettingDialog dialog = new SettingDialog();

          dialog.pack();
          dialog.setVisible(true);
        }
        catch (Exception e2) {
          log.error(ExceptionUtils.getStackTrace(e2));
        }
      });
      FlatDesktop.setQuitHandler(FlatDesktop.QuitResponse::performQuit);

    }

    Init.initTheme();

    // install inspectors
    FlatInspector.install("ctrl shift alt X");
    FlatUIDefaultsInspector.install("ctrl shift alt Y");


    mainFrame = new MainFrame();
    mainFrame.init();
    JPanel loadingPanel = new LoadingForm().getLoadingPanel();
    mainFrame.add(loadingPanel);
    mainFrame.pack();


    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    if (config.isDefaultMaxWindow() || screenSize.getWidth() <= 1366) {
      // The window is automatically maximized at low resolution
      mainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    }

    UpgradeUtil.smoothUpgrade();

    mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

    si = new oshi.SystemInfo();
    Init.initGlobalFont();

    if (!Init.configSucceed()) {
      SettingDialog dialog = new SettingDialog();
      dialog.pack();
      dialog.setVisible(true);
    }

    if ( Init.configSucceed()) {
//      mainFrame.setVisible(true);
      mainFrame.setContentPane(MainWindow.getInstance().getMainPanel());
      MainWindow.getInstance().init();
      Init.initAllTab();
      Init.initOthers();
      Init.startUploadSchedule();
      Init.startStateSchedule();
      mainFrame.remove(loadingPanel);
      //Runnable runnable = ()->{
      //  //Init.getDeptEquipAppList();
      //
      //};
      //executor.execute(runnable);
    } else {
      Init.stopUploadSchedule();
      Init.stopStateSchedule();
      mainFrame.dispose();
      System.exit(1);
    }


  }
}
